package com.yunzheng.exception;


import com.yunzheng.common.BaseResult;
import com.yunzheng.enums.ResultStatuesEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * <p>
 * 全局异常处理
 * </p>
 *
 * @author 郭思良
 * @create 2020-07-23 15:38
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    @ResponseBody
    public BaseResult systemExceptionHandler(SystemException e) {
        log.warn("System exception: ", e);
        return BaseResult.notOk(ResultStatuesEnums.GLOBAL_SYSTEM_EXCEPTION_HANDLER_ERROR);
    }


    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public BaseResult serviceExceptionHandler(ServiceException e) {
        log.debug("Service exception: ", e);
        if (e.getErrorEnum() == null) {
            return BaseResult.notOk(ResultStatuesEnums.FAIL, e.getMessage());
        } else {
            return BaseResult.notOk(e.getErrorEnum(), e.getMessage());
        }
    }


    /**
     * 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常，详情继续往下看代码
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public BaseResult bindExceptionHandler(BindException e) {
        log.debug("参数校验错误", e);
        return BaseResult.notOk(
                ResultStatuesEnums.BAD_PARAMETER,
                e.getBindingResult()
                        .getAllErrors()
                        .stream()
                        .map(DefaultMessageSourceResolvable::getDefaultMessage)
                        .collect(Collectors.joining(";"))
        );
    }


    /**
     * 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public BaseResult constraintViolationExceptionHandler(ConstraintViolationException e) {
        log.debug("参数校验错误", e);
        return BaseResult.notOk(
                ResultStatuesEnums.BAD_PARAMETER,
                e.getConstraintViolations()
                        .stream()
                        .map(ConstraintViolation::getMessage)
                        .collect(Collectors.joining(";")));
    }


    /**
     * 处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public BaseResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        log.debug("参数校验错误", e);
        return BaseResult.notOk(
                ResultStatuesEnums.BAD_PARAMETER,
                e.getBindingResult()
                        .getAllErrors()
                        .stream()
                        .map(DefaultMessageSourceResolvable::getDefaultMessage)
                        .collect(Collectors.joining(";")));
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResult unExceptedExceptionHandler(Exception e) {
        log.warn("Exception: ", e);
        return BaseResult.notOk(ResultStatuesEnums.GLOBAL_EXCEPTION_HANDLER_ERROR);
    }

}
